package com.example.administrator.liangcang.fragment;


import android.databinding.DataBindingUtil;
import android.graphics.pdf.PdfDocument;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.liangcang.BR;
import com.example.administrator.liangcang.R;
import com.example.administrator.liangcang.callback.LiangCangService;
import com.example.administrator.liangcang.daren.bean.DarenEntity;
import com.example.administrator.liangcang.databinding.FragmentDarenBinding;
import com.example.administrator.liangcang.until.BaseApplication;
import com.example.administrator.liangcang.until.CommentRecyclerAdapter;
import com.example.administrator.liangcang.viwe.MyPopupWindow;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class DarenFragment extends Fragment implements Callback<DarenEntity>,MyPopupWindow.onClickMyWindow {
      private FragmentDarenBinding binding;
    private LiangCangService service;
    private CommentRecyclerAdapter<Object> adapter;
    private int page=1;
    private MyPopupWindow window;

    public DarenFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return DataBindingUtil.inflate(inflater,R.layout.fragment_daren, container, false).getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding = DataBindingUtil.getBinding(view);
       aboutRecycler();
        aboutNextPage();
        aboutMenu();
    }

    private void aboutNextPage() {
        binding.recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                View item = recyclerView.getChildAt(recyclerView.getChildCount() - 1);
                int position = recyclerView.getChildAdapterPosition(item);
                if(position>=adapter.getItemCount()-3){
                    page = page+1;
                    service.getDarenEntity(page).enqueue(DarenFragment.this);
                }
            }
        });
    }


    private void aboutRecycler() {
        adapter = new CommentRecyclerAdapter<>(getContext(),new ArrayList<>(), R.layout.daren_item, BR.darenItem);
        GridLayoutManager manager = new GridLayoutManager(getContext(),3);
        binding.recycler.setLayoutManager(manager);
        binding.recycler.setAdapter(adapter);
        service = ((BaseApplication) getContext().getApplicationContext()).getService();
       service.getDarenEntity(page).enqueue(this);
    }

    @Override
    public void onResponse(Call<DarenEntity> call, Response<DarenEntity> response) {
            adapter.addAll(response.body().getData().getItems());
    }

    @Override
    public void onFailure(Call<DarenEntity> call, Throwable t) {
        Toast.makeText(getContext(), "DarenFragment"+t.getMessage(), Toast.LENGTH_SHORT).show();
    }

    private void aboutMenu() {
        binding.fragmentDarenMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 binding.fragmentDarenMenu.setImageResource(R.mipmap.close);
                if(window==null) {
                    window = new MyPopupWindow(getContext(), R.layout.daren_popwindow, v,350);
                }
                window.setCallback(DarenFragment.this);
                window.showWindow();
            }
        });
    }

    @Override
    public void onClickMyWindowListener(boolean isClose) {
        binding.fragmentDarenMenu.setImageResource(R.mipmap.actionbar_navigation_menu);
    }
    @Override
    public void onClickMyWindowButtonListener(String id) {
        Toast.makeText(getContext(), id, Toast.LENGTH_SHORT).show();
        adapter.clear();
        page = 1;
        if(id ==null){
            service.getDarenEntity(page);
        }else {
            service.getDarenEntity2(page, id).enqueue(this);
        }
    }
}
